﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_newstypelist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["login"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                YqhData obj = new YqhData();
                ddltype.DataSource = obj.FillDataSet("select * from newstype order by tid");
                ddltype.DataTextField = "tname";
                ddltype.DataValueField = "tid";
                ddltype.DataBind();
            }
        } 
    }

    private void rptdatabind()
    {
        YqhData obj = new YqhData();
        string count = string.Format("select count(*) from news where ntypeid = '{0}'", ddltype.SelectedValue);
        AspNetPager1.RecordCount = Convert.ToInt32(obj.ExecuteScalar(count));
        int pageindex = AspNetPager1.CurrentPageIndex - 1;
        int pagesize = AspNetPager1.PageSize;
        string sql = string.Format(@"select NID,naddname,tname,nsubject=substring(nsubject,0,20) +                                                     '...',ndate from
                                                    news inner join newstype 
                                                    on 
                                                    news.ntypeid = newstype.tid
                                                    where ntypeid = {0}
                                                    order by ndate desc", ddltype.SelectedValue.ToString());
        rptnewslist.DataSource = obj.ExecuteDataSet(sql,pageindex,pagesize);
        rptnewslist.DataBind();
    }
    protected void shou_Click(object sender, EventArgs e)
    {
        rptdatabind();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        rptdatabind();
    }
    protected void rptnewslist_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        YqhData obj = new YqhData();
        switch (e.CommandName.ToLower())
        {
            case "del":
                string sql = string.Format("delete news where nid={0}", e.CommandArgument);
                obj.ExecuteSql(sql);
                rptdatabind();
                break;
            case "e":
                Response.Redirect(string.Format("newsedit.aspx?op=e&id={0}", e.CommandArgument));
                break;
        }
    }
}
